package com.qcby.llmtchstd.controller;

import com.qcby.llmtchstd.common.ResponseResult;
import com.qcby.llmtchstd.entity.Student;
import com.qcby.llmtchstd.entity.Teacher;
import com.qcby.llmtchstd.entity.User;
import com.qcby.llmtchstd.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 教师登录控制器
 * 处理教师登录相关请求，对应接口文档中用户端的教师登录接口
 */
@RestController
@RequestMapping("")  // 类级别路径映射
public class UserLoginController {

    @Autowired
    private UserLoginService userLoginService;
    private final Map<Integer, User> sessionMap = new HashMap<>();

    @PostMapping("/teacher/login")  // 方法级别路径映射，完整路径为/teacher/login
    public ResponseResult<Teacher> teacherLogin(@RequestBody Teacher teacher, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = new User();
        loginUser.setName(teacher.getName());
        loginUser.setPassword(teacher.getPassword());
        loginUser.setType(1);
        if(session.getAttribute("loginUser") != null){
            session.removeAttribute("loginUser");
        }
        // 调用服务层处理登录逻辑
        Teacher loginTeacher = userLoginService.teacherLogin(teacher.getName(), teacher.getPassword());
        loginUser.setId(loginTeacher.getId());
        if (loginTeacher != null) {
            session.setAttribute("teacherId", loginTeacher.getId());
            session.setAttribute("loginUser", loginUser);
            sessionMap.put(loginUser.getId(), loginUser);
            System.out.println("新用户诞生了：" + loginUser);

            loginTeacher.setPassword("******");  // 密码脱敏
            return ResponseResult.success("登录成功", loginTeacher);
        } else {
            // 登录失败，返回失败响应
            return ResponseResult.fail("4002", "密码错误");
        }
    }


    @PostMapping("/student/login")  // 方法级别路径映射，完整路径为/teacher/login
    public ResponseResult<Student> studentLogin(@RequestBody Student student, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = new User();
        loginUser.setName(student.getName());
        loginUser.setPassword(student.getPassword());
        loginUser.setType(0);
        if(session.getAttribute("loginUser") != null){
            session.removeAttribute("loginUser");
        }
        // 调用服务层处理登录逻辑
        Student loginStudent = userLoginService.studentLogin(student.getName(), student.getPassword());
        loginUser.setId(loginStudent.getId());
        if (loginStudent != null) {
            session.setAttribute("loginUser", loginUser);
            sessionMap.put(loginUser.getId(), loginUser);
            System.out.println("新用户诞生了：" + loginUser);
            // 登录成功，返回成功响应（包含教师信息，密码脱敏处理）
            loginStudent.setPassword("******");  // 密码脱敏
            return ResponseResult.success("登录成功", loginStudent);
        } else {
            // 登录失败，返回失败响应
            return ResponseResult.fail("4002", "密码错误");
        }
    }

    @PostMapping("/user/logout")  // 方法级别路径映射，完整路径为/teacher/login
    public ResponseResult<User> logout(HttpSession session) {
        session.removeAttribute("loginUser");
        session.invalidate();
        return ResponseResult.success("退出登录成功", null);
    }
    @GetMapping("/user/studentCurrent")
    public ResponseResult<Student> studentCurrent(HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        int id = loginUser.getId();
        Student student = userLoginService.studentCurrent(id);
        return ResponseResult.success("查询成功", student);
    }
    @GetMapping("/user/teacherCurrent")
    public ResponseResult<Teacher> teacherCurrent(HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        int id = loginUser.getId();
        Teacher teacher = userLoginService.teacherCurrent(id);
        return ResponseResult.success("查询成功", teacher);
    }
}
